package com.audaque.springboot.foshanupload.web.websocketdemo.controller;

import com.audaque.springboot.foshanupload.web.websocketdemo.component.WebSocketServer;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;

/**
 http://localhost:8081/websocketdemo/api/socket/index/1
 * @ Auther: zgb

 */
@Controller
@RequestMapping("/socket")
public class SocketController {
    //页面请求
    @GetMapping("/index/{userId}")
    public ModelAndView index(@PathVariable String userId) {
        ModelAndView mav = new ModelAndView("index");
        mav.addObject("userId", userId);
        return mav;
    }

    //推送数据接口
    @ResponseBody
    @RequestMapping("/push/{clientId}")
    public void push(@PathVariable String clientId, String message) throws IOException {
        WebSocketServer.sendInfo(message, clientId);
    }
}
